package com.gxd.business.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxd.business.domain.Customer;
import com.gxd.business.domain.qco.CustomerQco;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author gxd
 * @since 2025/6/13
 */
public interface CustomerMapper extends BaseMapper<Customer> {

    /**
     * 分页列表-资源客户列表
     * @param page 分页信息
     * @param qco 资源客户查询条件
     * @return 分页列表
     */
    @Select({"<script> ",
            " select * ",
            " from t_customer ",
            " where deleted = 0 ",
            "  <if test=\"qco.customerName != null and qco.customerName != ''\"> ",
            "   and customer_name like concat('%', #{qco.customerName}, '%')",
            "  </if> ",
            "  <if test=\"qco.phone != null and qco.phone != ''\"> ",
            "    and phone like concat('%', #{qco.phone}, '%')",
            "  </if> ",
            "  <if test=\"qco.region != null and qco.region != ''\"> ",
            "    and region like concat('%', #{qco.region}, '%')",
            "  </if> ",
            "  <if test=\"qco.assignStatus != null\" >",
            "    and assign_status = #{qco.assignStatus}",
            "  </if> ",
            "  <if test=\"qco.intentionCategory != null and qco.intentionCategory != ''\"> ",
            "    and intention_category in ",
            "    <foreach collection=\"qco.intentionCategoryList\" item=\"item\" index=\"index\" open=\"(\" close=\")\" separator=\",\"> ",
            "       #{item}",
            "    </foreach> ",
            "  </if> ",
            "</script>"})
    Page<Customer> pageList(Page<CustomerQco> page, @Param("qco") CustomerQco qco);

    /**
     * 分页列表-我的客户列表
     * @param page 分页信息
     * @param qco 资源客户查询条件
     * @return 分页列表
     */
    @Select({"<script> ",
            " select * ",
            " from t_customer t",
            " where EXISTS(",
            "    select 1 ",
            "    from t_allocation_customer_log log1 ",
            "    where log1.customer_id = t.id ",
            "    <if test= \"qco.userId != null\"> ",
            "       and log1.to_user_id = #{qco.userId}",
            "    </if> ",
            "    and log1.assign_time = (select max(assign_time) from t_allocation_customer_log log2 where log2.customer_id = log1.customer_id)",
            " ) and t.deleted = 0 ",
            "  <if test=\"qco.customerName != null and qco.customerName != ''\"> ",
            "   and t.customer_name like concat('%', #{qco.customerName}, '%')",
            "  </if> ",
            "  <if test=\"qco.phone != null and qco.phone != ''\"> ",
            "    and t.phone like concat('%', #{qco.phone}, '%')",
            "  </if> ",
            "  <if test=\"qco.region != null and qco.region != ''\"> ",
            "    and t.region like concat('%', #{qco.region}, '%')",
            "  </if> ",
            "  <if test=\"qco.intentionLevel != null\"> ",
            "    and t.intention_level = #{qco.intentionLevel}",
            "  </if> ",
            "  <if test=\"qco.assignStatus != null\"> ",
            "    and t.assign_status = #{qco.assignStatus}",
            "  </if> ",
            "  <if test=\"qco.intentionCategory != null and qco.intentionCategory != ''\"> ",
            "    and t.intention_category in ",
            "    <foreach collection=\"qco.intentionCategoryList\" item=\"item\" index=\"index\" open=\"(\" close=\")\" separator=\",\"> ",
            "       #{item}",
            "    </foreach> ",
            "  </if> ",
            "</script>"})
    Page<Customer> myPageList(Page<CustomerQco> page, @Param("qco") CustomerQco qco);

    /**
     * 查询分配三天没加联系方式的资源客户Id集合
     * @return 资源客户id集合
     */
    @Select({"<script> ",
            " select t.id ",
            " from t_customer t ",
            " where EXISTS( ",
            "   select 1 ",
            "   from t_allocation_customer_log ",
            "   where customer_id = t.id <![CDATA[ and assign_time <= NOW() - INTERVAL 3 DAY ]]>",
            " ) ",
            " AND t.deleted = 0 and t.assign_status = 1 and IFNULL(t.contact_status, 1) = 1",
            "</script>"})
    List<String> selectDiff3DaysIdList();


    /**
     * 根据资源客户Id和用户Id获取客户资源
     * @param customerId 资源客户Id
     * @param userId 用户Id
     * @return 结果
     */
    @Select({"<script> ",
            " select * ",
            " from t_customer t",
            " where EXISTS(",
            "    select 1 ",
            "    from t_allocation_customer_log log1 ",
            "    where log1.customer_id = t.id ",
            "    and log1.to_user_id = #{userId}",
            "    and log1.assign_time = (select max(assign_time) from t_allocation_customer_log log2 where log2.customer_id = log1.customer_id)",
            " ) and t.deleted = 0 and t.assign_status = 1 and t.id = #{customerId}",
            "</script>"})
    Customer getCustomerByIdAndUserId(@Param("customerId") String customerId, @Param("userId") Long userId);
}
